
package com.xebest.util;

import com.xebest.constant.XeConstans;
import com.xebest.web.model.XeVenOrderInfoDetailEntity;
import com.xebest.web.model.XeVenOrderInfoEntity;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * ERP计算公式工具类
 *
 * @author 李贝贝
 */
public class CalUtils {

    /**
     * 商品明细实发金额计算
     * 差异量=购买数量-实发量
     * 商品级：商品订购金额=购买数量×成交价
     * 商品级：商品优惠后总价=商品订购金额-商品优惠金额
     * 商品级：商品优惠后单价=商品优惠后总价÷购买数量=[商品订购金额-商品优惠金额]÷购买数量
     * 商品级：商品差异金额=商品优惠后单价×差异量
     * 商品级：商品实发金额=商品优惠后总价-商品差异金额
     */
    public static XeVenOrderInfoDetailEntity getProDifferenceAmount(XeVenOrderInfoDetailEntity orderInfoDetail) {
        if (orderInfoDetail.getPurchaseNum().compareTo(orderInfoDetail.getRealSendNum()) != XeConstans._XE_ZERO) {
            //优惠后总价
            BigDecimal freTotalAmout = orderInfoDetail.getProTotalPrice()
                    .subtract(orderInfoDetail.getPmtAmount() == null ? new BigDecimal("0") : orderInfoDetail.getPmtAmount());
            //商品差异金额
            BigDecimal differenceAmout = freTotalAmout.divide(orderInfoDetail.getPurchaseNum(), 4, BigDecimal.ROUND_HALF_EVEN).multiply(orderInfoDetail.getPurchaseNum().subtract(orderInfoDetail.getRealSendNum()));
            orderInfoDetail.setDifferenceAmount(differenceAmout.setScale(2, RoundingMode.HALF_UP));
        }
        return orderInfoDetail;
    }

    /**
     * 运费退款金额
     * 订购总价=商品订购金额之和
     * 优惠总额=优惠金额
     * 订单优惠后总价=订购总价-优惠总价
     * 差异运费金额=运费总额×（订单差异金额÷订单优惠后总价）
     */
    public static BigDecimal getProDifferenceRefundAmount(BigDecimal orderDifferenceAmount, XeVenOrderInfoEntity orderInfoEntity) {
        BigDecimal freDifferenceAmount = new BigDecimal("0");
        if (orderDifferenceAmount.compareTo(BigDecimal.ZERO) != 0) {
            //优惠总额
            BigDecimal pmtAmout = orderInfoEntity.getPmtAmount() == null ? new BigDecimal("0") : orderInfoEntity.getPmtAmount();
            //差异金额       运费*（差异总金额/（订单总价*优惠金额））
            BigDecimal divide = orderDifferenceAmount.divide(orderInfoEntity.getGoodsAmount().subtract(pmtAmout), 4, BigDecimal.ROUND_HALF_EVEN);
            freDifferenceAmount = (orderInfoEntity.getFreAmount() == null ? new BigDecimal("0") : orderInfoEntity.getFreAmount()
                    .multiply(divide));
        }
        return freDifferenceAmount.setScale(2, RoundingMode.HALF_UP);
    }

    /**
     * 12、订单级：订单优惠后总价=订购总价-优惠总价
     * 13、订单级：订单实发金额=商品实发金额之和=订单优惠后总价-订单差异金额
     * 14、订单级：实发运费金额 =运费总额×（订单实发金额÷订单优惠后总价）
     * 15、订单级：差异运费金额=运费总额×（订单差异金额÷订单优惠后总价）
     * 16、应收金额=订单实发金额+实发运费金额
     */
    public static BigDecimal getPayAmount(XeVenOrderInfoEntity orderInfoEntity, BigDecimal orderDifferenceAmount) {
        orderInfoEntity.setFreAmount(orderInfoEntity.getFreAmount() == null ? new BigDecimal("0") : orderInfoEntity.getFreAmount());
        orderInfoEntity.setPmtAmount(orderInfoEntity.getPmtAmount() == null ? new BigDecimal("0") : orderInfoEntity.getPmtAmount());
        //订单实发金额
        BigDecimal payAmount = orderInfoEntity.getPayAmount().subtract(orderDifferenceAmount).subtract(orderInfoEntity.getFreAmount());
        //订单实发运费金额
        BigDecimal payFreAmount = orderInfoEntity.getFreAmount()
                .multiply(payAmount.divide((orderInfoEntity.getGoodsAmount().subtract(orderInfoEntity.getPmtAmount())), 4, BigDecimal.ROUND_HALF_EVEN));
        //应收金额
        payAmount = payAmount.add(payFreAmount);
        return payAmount.setScale(2, RoundingMode.HALF_UP);
    }
}
  
